<template>
  <a-modal
      title="租借卡充值"
      :width="600"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleSubmit"
      @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
                label="用户名"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
            >
              <a-input
                  v-model="user.loginName"
                  disabled/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
                label="昵称"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
            >
              <a-input
                  v-model="user.userName"
                  disabled/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
                label="租借卡号"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
            >
              <a-input
                  v-model="user.cardNumber"
                  disabled/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
                label="充值金额"
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
            >
              <a-input
                  v-decorator="['money', {initialValue: 0, rules: [
                  { required: true, message: '请输入充值金额', trigger: 'blur' },]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :span="24">
            <a-form-item
                label="备注"
                :labelCol="{
                  xs: 24,
                  sm: 4
                }"
                :wrapperCol="{
                  xs: 24,
                  sm: 19
                }"
            >
              <a-textarea
                  v-decorator="['note', {rules: []}]"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import {editUser} from '@/api/basic/user'
import {addCardRecord} from "@/api/bike/cardRecord";

export default {
  props: {},
  data () {
    return {
      labelCol: {
        xs: {span: 24},
        sm: {span: 7}
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16}
      },
      visible: false,
      confirmLoading: false,
      user: {},

      form: this.$form.createForm(this)
    }
  },
  methods: {
    pay (record) {
      this.visible = true
      this.user = record
    },
    handleSubmit () {
      const {form: {validateFields}} = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          let cardRecord = {
            userId: this.user.userId,
            cardId: this.user.cardId,
            cardNumber: this.user.cardNumber,
            money: values.money,
            note: values.note,
            recordType: 1,
          }
          addCardRecord(cardRecord)
              .then(() => {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              }).catch(err => {
            this.confirmLoading = false
            this.$message.error(err.result || '操作失败！')
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
